useradd command
useradd
- create a new user or update default new user information
The useradd
command in Linux is a utility used to create new user accounts on a system. It’s a low-level tool for adding users, often followed by passwd
to set their password.
Note: useradd
requires root privileges (sudo
). On some systems, adduser
(a friendlier wrapper) is preferred, but useradd
is more granular.
Usage: useradd [options] LOGIN
LOGIN
: This is the required argument, which specifies the username (login name) for the new account.options
: Flags to customize the user account.
Common Options Summary
Option | Description |
---|---|
-m | Create home directory |
-d | Specify home directory |
-s | Set login shell (e.g., /bin/bash ) |
-g | Set primary group |
-G | Add to supplementary groups |
-c | Add a comment (e.g., full name) |
-u | Set custom UID |
Examples
-
Basic Usage
Create a new user with default settings.
sudo useradd alice
- Adds user
alice
to the system. - Defaults (e.g., home directory, shell) come from
/etc/default/useradd
.
Set Password:
sudo passwd alice
- Prompts for a password for
alice
.
- Adds user
-
Specifying Home Directory
Use
-d
to set a custom home directory.sudo useradd -d /home/alice alice
- Sets
/home/alice
as the home directory (won’t create it yet).
Create Home Directory:
Add
-m
to make the home directory:sudo useradd -m -d /home/alice alice
- Creates
/home/alice
and sets it.
- Sets
-
Setting a Shell
Use
-s
to specify the login shell.sudo useradd -m -s /bin/bash alice
- Sets
bash
asalice
’s shell (default is often/bin/sh
).
- Sets
-
Assigning a Group
Use
-g
for the primary group and-G
for additional groups.sudo useradd -m -g users -G developers alice
-g users
: Primary group isusers
.-G developers
: Addsalice
to thedevelopers
group.
-
Adding a Comment
Use
-c
to add a comment (e.g., full name).sudo useradd -m -c "Alice Smith" alice
- Stores "Alice Smith" in
/etc/passwd
’s GECOS field.
- Stores "Alice Smith" in
-
Setting User ID
Use
-u
to specify a custom UID.sudo useradd -m -u 1500 alice
- Assigns UID 1500 to
alice
(default starts at 1000+).
- Assigns UID 1500 to
-
Checking the Result
Verify the user in
/etc/passwd
.grep alice /etc/passwd
- Output:
alice:x:1500:100:Alice Smith:/home/alice:/bin/bash
.
- Output:
To get help related to the useradd
command use --help
option
$ useradd --help
Usage: useradd [options] LOGIN
useradd -D
useradd -D [options]
Options:
--badnames do not check for bad names
-b, --base-dir BASE_DIR base directory for the home directory of the
new account
--btrfs-subvolume-home use BTRFS subvolume for home directory
-c, --comment COMMENT GECOS field of the new account
-d, --home-dir HOME_DIR home directory of the new account
-D, --defaults print or change default useradd configuration
-e, --expiredate EXPIRE_DATE expiration date of the new account
-f, --inactive INACTIVE password inactivity period of the new account
-g, --gid GROUP name or ID of the primary group of the new
account
-G, --groups GROUPS list of supplementary groups of the new
account
-h, --help display this help message and exit
-k, --skel SKEL_DIR use this alternative skeleton directory
-K, --key KEY=VALUE override /etc/login.defs defaults
-l, --no-log-init do not add the user to the lastlog and
faillog databases
-m, --create-home create the user's home directory
-M, --no-create-home do not create the user's home directory
-N, --no-user-group do not create a group with the same name as
the user
-o, --non-unique allow to create users with duplicate
(non-unique) UID
-p, --password PASSWORD encrypted password of the new account
-r, --system create a system account
-R, --root CHROOT_DIR directory to chroot into
-P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files
-s, --shell SHELL login shell of the new account
-u, --uid UID user ID of the new account
-U, --user-group create a group with the same name as the user
-Z, --selinux-user SEUSER use a specific SEUSER for the SELinux user mapping
--extrausers Use the extra users database
For more details, check the manual with man useradd